<?php

namespace App\Http\Controllers\Home;

use App\Http\Controllers\Controller;
use App\Home\Lession;
use Input;
use Auth;

class LessionController extends Controller
{
    public function checkLogin(){
        $user = Auth::guard('home')->user();
        if(empty($user) == true){
            $data = array(
                'errorMessage' => '尚未登录！',
                'success'      => false,
            );
            return response() -> json($data);
        }
    }

    //精品微课和免费微课的视图
    public function fineClass(){
        return view('home.lession.course');
    }

    //获取精品微课的列表
    public function getMicroCourses($pageNumber,$pageSize,$directionId,$orderType,$classType){
        $offset = ($pageNumber - 1) * $pageSize;
        $order = $orderType == 0 ? 'sort' : ($orderType == 1 ? 'created_at' : ($orderType == 2 ? 'click_num' : 'id'));
        $free = $classType == 'micro' ? 'false' : ($classType == 'free' ? 'true' : 'false');
        $Coursebelong = $classType == 'micro' ? '3' : ($classType == 'free' ? '4' : '3');
        if($directionId == 0){
            $resultObject = Lession::select('id','lession_name','cover_img','price','free','courseBelong','video_time','click_num','recommend_type')
                -> orderBy($order,'desc')
                -> whereStatusAndFreeAndCoursebelong('2',$free,$Coursebelong)
                -> offset($offset)
                -> limit($pageSize)
                -> get();
            $totalCount = Lession::select('id') -> whereStatusAndFreeAndCoursebelong('2',$free,$Coursebelong) -> count();
        }else{
            $resultObject = Lession::select('id','lession_name','cover_img','price','free','courseBelong','video_time','click_num','recommend_type')
                -> orderBy($order,'desc')
                -> whereCourse_idAndStatusAndFreeAndCoursebelong($directionId,'2',$free,$Coursebelong)
                -> offset($offset)
                -> limit($pageSize)
                -> get();
            $totalCount = Lession::select('id') -> whereCourse_idAndStatusAndFreeAndCoursebelong($directionId,'2',$free,$Coursebelong) -> count();
        }

        foreach($resultObject as $key => $value){
            $resultObject[$key]['del_price'] = $value['price'] + 1599;
        }

        $totalPageCount = ceil($totalCount/$pageSize);
        $page = array(
            'pageSize'       => $pageSize,         //每页显示条数
            'totalPageCount' => $totalPageCount,   //总页数
            'currentPage'    => $pageNumber,       //当前页
            'totalCount'     => $totalCount,       //总条数
        );
        if(!$resultObject -> isEmpty()){
            $data = compact('resultObject','page');
        }else{
            $data['success'] = false;
        }
        return response() -> json($data);
    }

    //播放地址
    public function getVideo(){
        $this -> checkLogin();
        $courseId = Input::get('courseId');
        if(empty($courseId) == true){
            $data['status'] = 0;
            return response() -> json($data);
        }
        $lessionId = Input::get('lessionId');
        if(empty($lessionId) == true){
            if(Auth::guard('home') -> user() == null){
                $res = Lession::select('id','lession_name','video_addr','HD_video_addr','SC_video_addr','courseBelong')
                    -> whereCourse_idAndStatus($courseId,2)
                    -> orderBy('id','asc')
                    -> first()
                    -> toArray();
            }else{
                $watch_id = Member_watch_the_record::whereCourse_idAndMember_id($courseId,Auth::guard('home') -> user() -> id) -> value('lession_id');
                if($watch_id){
                    $res = Lession::select('id','lession_name','video_addr','HD_video_addr','SC_video_addr','courseBelong')
                        -> whereId($watch_id)
                        -> first()
                        -> toArray();
                }else{
                    $res = Lession::select('id','lession_name','video_addr','HD_video_addr','SC_video_addr','courseBelong')
                        -> whereCourse_idAndStatus($courseId,2)
                        -> orderBy('id','asc')
                        -> first()
                        -> toArray();
                }
            }
        }else{
            if(Auth::guard('home') -> user() == null){
                $res = Lession::select('id','lession_name','video_addr','HD_video_addr','SC_video_addr','courseBelong')
                    -> whereId($lessionId)
                    -> first()
                    -> toArray();
            }else{

            }

        }

        if(empty($res) == true){
            $data['status'] = 0;
        }else{
            $data = array(
                'status'  => 200,
                'message' => 'success',
                'result'  => [
                    'videoId'         => $res['id'],
                    'videoName'       => $res['lession_name'],
                    'playCode'        => "http://p1eawv43q.bkt.clouddn.com/upload2017-12-25_5a40da5e3d2d7.mp4",
                    'playCode_HD'     => $res['HD_video_addr'],
                    'playCode_SC'     => $res['SC_video_addr'],
                    'currentPlayType' => $res['courseBelong'],
                ],
            );
        }
        return response() -> json($data);


    }

    //问答权限
    public function checkAsk(){
        $this -> checkLogin();
    }

    //笔记
    public function getNotes(){
        $this -> checkLogin();
    }

}